#include "include/ShowLyric.h"
#include "include/ShowLyricApp.h"
#include <sys/stat.h>

#include "include/ConfigDlg.h"
#include "include/ShowLyricDlg.h"
#include "include/SelLyricDlg.h"
#include "include/ManualDownLoadDlg.h"
#include "include/DownLoadLyric.h"

struct _ShowLyricApp theApp =
{ 	.Init = Init,
	.Clean = Clean,
	.ShowConfigDlg = ShowConfigDlg,
	.UpdataSetting = UpdataSetting,
	.PreChangeSong = PreChangeSong,
	.ChangeSong = ChangeSong,
	.About = About,
	.GetDefautConfigs = GetDefautConfigs
};
gchar* GetDefaultLyricPath(OUT gchar * lpszPath)
{
	if (!lpszPath)
		return NULL;
	sprintf(lpszPath, "%s/.lyrics", theApp.m_szCurrUsrPath);
	if (access(lpszPath, F_OK) == -1)
	{
		mkdir(lpszPath, 0755);
	}
	return lpszPath;
}
// 获取默认设置
void GetDefautConfigs(Configs* pConfig)
{
	pConfig->bShowWindowLyric = TRUE;
	pConfig->bSmartDownLoad = TRUE;
	pConfig->bSmartShowWin = TRUE;
	pConfig->bChangeLyric = TRUE;
	pConfig->bCanDrag = TRUE;
	pConfig->bAutoSaveLyric = FALSE;
	pConfig->bShowBroad = FALSE;
	pConfig->bSaveLyricToSongFolder = FALSE;
	pConfig->bSaveLyricAsFileName = TRUE;
	pConfig->bShowOSDLyric = TRUE;
	pConfig->bIsKaLaOK = TRUE;
	strcpy(pConfig->OSD.fake_translucent_bg, "on");
	strcpy(pConfig->OSD.vposition, "top");
	strcpy(pConfig->OSD.halignment, "center");
	strcpy(pConfig->OSD.animations, "off");
	strcpy(pConfig->OSD.szFontName, "Sans 32");
	strcpy(pConfig->szLyricFontName, "Sans 14");
	strcpy(pConfig->szLyricEncode, "GBK");
	pConfig->nSearchAppIndex = 0;
	GetDefaultLyricPath(pConfig->szLyricPath);
	gdk_color_parse("black", &pConfig->colors.background);
	gdk_color_parse("darkblue", &pConfig->colors.normal);
	gdk_color_parse("green", &pConfig->colors.current);
	gdk_color_parse("yellow", &pConfig->colors.drag);
	gdk_color_parse("green", &pConfig->colors.msg);
	gdk_color_parse("red", &pConfig->colors.error);
}

void Init()
{
	LyricDebug("theApp:Init");
	theApp.m_state = AS_Waiting;
	struct passwd *my_info = getpwuid(getuid());
	strcpy(theApp.m_szCurrUsrPath, my_info->pw_dir);
	sprintf(theApp.m_szConfigPath, "%s/.%s", theApp.m_szCurrUsrPath, 
	PLUGIN_NAME);
	if (access(theApp.m_szConfigPath, F_OK) == -1)
	{
		mkdir(theApp.m_szConfigPath, 0755);
	}
	// 写入默认配置
	GetDefautConfigs(&theApp.m_configs);

	// 初始化默认引擎
	if (theApp.m_player.GetSearchAppCount() == 0)
	{
		AppInfo info;
		int index = 0;
		strcpy(info.szApp, "sogou");
		strcpy(info.szName, "搜狗");
		theApp.m_player.SetSearchApp(index++, info);
		strcpy(info.szApp, "ttplayer");
		strcpy(info.szName, "千千静听");
		theApp.m_player.SetSearchApp(index++, info);
		strcpy(info.szApp, "baidu");
		strcpy(info.szName, "百度");
		theApp.m_player.SetSearchApp(index++, info);
		strcpy(info.szApp, "google");
		strcpy(info.szName, "谷歌");
		theApp.m_player.SetSearchApp(index++, info);
		strcpy(info.szApp, "lrc123");
		strcpy(info.szName, "lrc123");
		theApp.m_player.SetSearchApp(index++, info);
	}

	// 加载配置信息
	theApp.m_player.LoadConfigs(&theApp.m_configs);

	// 初始化界面
	theApp.m_xml = glade_xml_new(GLADE_FILE, NULL, NULL);
	 if (!g_thread_supported()) 
	      g_thread_init(NULL); 
	gdk_threads_init();
	LyricWndInit();
	UpdataSetting();
}

// 释放资源
void Clean()
{
	// 释放资源
	ClearLyric();
}

// 显示配置窗口
void ShowConfigDlg()
{
	GladeXML *xml = glade_xml_new(CONFIG_GLADE_FILE, NULL, NULL);
	gtk_widget_show(ConfigDlgInit(xml));
}

// 更新配置信息
void UpdataSetting()
{
	theApp.m_player.SaveConfigs(&theApp.m_configs);
	theApp.m_LyricWnd.RefreshLyricSetting();
}

// 释放链表占用的资源
void _FreeList(GList* lpList)
{
	GList* list = g_list_first(lpList);
	while (list)
	{
		g_free(list->data);
		list = g_list_next(list);
	}
	g_list_free(lpList);
}

// 加载歌词
gboolean _LoadLyric(SongInfo info)
{
	const gchar * pszTitle = info.szTitle;
	const gchar * pszArtist = info.szArtist;
	LyricDebug("LoadLyric(%s, %s)", pszTitle, pszArtist);
#define LRC_PATH_NUM	12
	gchar szLrcFile[LRC_PATH_NUM][MAX_PATH];
	if (strlen((char *) pszTitle) <= 0)
		return FALSE;
	int nCount = 0;
	sprintf(szLrcFile[nCount++], "%s/%s/%s.lrc", theApp.m_configs.szLyricPath, pszArtist, pszTitle);
	sprintf(szLrcFile[nCount++], "%s/%s - %s.lrc", theApp.m_configs.szLyricPath, pszArtist, pszTitle);
	sprintf(szLrcFile[nCount++], "%s/%s.lrc", theApp.m_configs.szLyricPath, pszTitle);
	sprintf(szLrcFile[nCount++], "%s/%s/%s.lrc", info.szSongFolder, pszArtist, pszTitle);
	sprintf(szLrcFile[nCount++], "%s/%s - %s.lrc", info.szSongFolder, pszArtist, pszTitle);
	sprintf(szLrcFile[nCount++], "%s/%s.lrc", info.szSongFolder, pszTitle);
	gchar szTmp[MAX_PATH] = {0};
	sprintf(szLrcFile[nCount++], "%s/%s.lrc", info.szSongFolder, GetNameFromFileName(info.szFileName, szTmp, MAX_PATH));
	sprintf(szLrcFile[nCount++], "%s/%s.lrc", theApp.m_configs.szLyricPath, GetNameFromFileName(info.szFileName, szTmp, MAX_PATH));
	szLrcFile[LRC_PATH_NUM - 1][0] = 0;
	// 检查该歌曲的歌词是否存在
	int i = 0;
	for (i = 0; i < nCount; i++)
	{
		if (g_file_test(szLrcFile[i], G_FILE_TEST_IS_REGULAR))
		{
			break;
		}
	}
	theApp.m_CurrSongInfo = info;
	if (i < nCount)
	{
		strcpy(theApp.m_CurrSongInfo.szLyricFileName, szLrcFile[i]);
		return theApp.m_LyricWnd.ParseLyric(theApp.m_CurrSongInfo.szLyricFileName);
	}
	else
	{
		theApp.m_CurrSongInfo = info;
		return SearchLyric(info, theApp.m_configs.bSmartDownLoad, theApp.m_configs.nSearchAppIndex, TRUE);
	}
}

gboolean PreChangeSong(SongInfo* pInfo)
{
	theApp.m_LyricWnd.ClearLyric();
	gtk_window_set_title(GTK_WINDOW(theApp.m_LyricWnd.pThis), PLUGIN_NAME);
	gboolean bRet = _LoadLyric(*pInfo);
	theApp.m_LyricWnd.SmartShowWnd(bRet);
	return bRet;
}

void ChangeSong(SongInfo info)
{
	gtk_window_set_title(GTK_WINDOW(theApp.m_LyricWnd.pThis), info.szTitle);
}

void About(GtkWindow* pParent)
{
	const char *authors[] =
	{ "Allan <qimingos_lsk@163.com>", NULL };
	const gchar *license[] =
	{ "This is free software; you can redistribute it and/or modify "
		"it under the terms of the GNU General Public License as published by "
		"the Free Software Foundation; either version 2 of the License, or "
		"(at your option) any later version.",
			"It is distributed in the hope that it will be useful, "
				"but WITHOUT ANY WARRANTY; without even the implied warranty of "
				"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the "
				"GNU General Public License for more details.",
			"You should have received a copy of the GNU General Public License "
				"along with this; if not, write to the Free Software Foundation, Inc., "
				"51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA" };
	gchar *license_text = g_strjoin("\n\n", 
	_(license[0]), _(license[1]), _(license[2]), NULL);
	gtk_show_about_dialog(pParent, "name", PLUGIN_NAME, "comments", 
	PLUGIN_NAME, "program-name", PLUGIN_NAME, "version", VERSION, "authors", authors, "license",
			license_text, "wrap-license", TRUE, "translator-credits", _("translator-credits"), "logo-icon-name", PLUGIN_NAME, 
			NULL);
	g_free(license_text);
}
